import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

public class TextBuddyTest {

  String fileName;
  File file;

  @Before
  public void setup() {
    fileName = "bin/test/unitTestFile";
    file = new File(fileName);
  }

  @Test
  public void testParseTokens() {
    String result = "";
    result = TextBuddy.combineTokens(new String[] { "test", "token", "1" });
    assertEquals("test parse tokens", result, "token 1");
  }

  @Test
  public void testAddLineToFile() {
    // Prepare file for test: clear test file
    try {
      FileWriter writer = new FileWriter(file);
      writer.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    // Test: adding new line
    TextBuddy.addLineToFile(file, "add testing add line to file".split(" "));

    try {
      BufferedReader reader = new BufferedReader(new FileReader(file));
      assertEquals("test addLineToFile", reader.readLine(), "testing add line to file");
      reader.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

  }

  @Test
  public void testDeleteLine() {
    // Prepare file for tests below
    try {
      BufferedWriter writeFile = new BufferedWriter(new FileWriter(file));
      writeFile.write("line1");
      writeFile.newLine();
      writeFile.write("line2");
      writeFile.newLine();
      writeFile.close();

    } catch (IOException ex) {
      ex.printStackTrace();
    }
    // Test: line does not exist error
    assertEquals("line does not exist error", "ERROR: Line 3 does not exist!", TextBuddy.deleteLine(file, fileName, "3"));
    // TEst: line successful delete
    assertEquals("successful line delete", "deleted from " + fileName
	+ ": \"line2\"", TextBuddy.deleteLine(file, fileName, "1"));

  }

  @Test
  public void testSort() {
    // test if sort() exists
    TextBuddy.sort(file);

    // test sort() functionality
    try {
      BufferedWriter writeFile = new BufferedWriter(new FileWriter(file));
      writeFile.write("ccc");
      writeFile.newLine();
      writeFile.write("b");
      writeFile.newLine();
      writeFile.write("1");
      writeFile.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    TextBuddy.sort(file);
    try {
      BufferedReader readFile = new BufferedReader(new FileReader(file));
      assertEquals("file sort error", "1", readFile.readLine());
      assertEquals("file sort error", "b", readFile.readLine());
      assertEquals("file sort error", "ccc", readFile.readLine());
      readFile.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

  }

  @Test
  public void testSearch() {
    // test if search() exists
    ArrayList<String> lines = TextBuddy.search(file, new String[] { "search",
	"hello" });

    // test search() functionality
    TextBuddy.clearFile(file, fileName);
    TextBuddy.addLineToFile(file, "search this is a search string xxx".split(" "));
    TextBuddy.addLineToFile(file, "search another line".split(" "));
    TextBuddy.addLineToFile(file, "search tsdddsasad asddsad dsadasd".split(" "));
    TextBuddy.addLineToFile(file, "search The movie xxx".split(" "));
    TextBuddy.addLineToFile(file, "search this is a search string".split(" "));
    lines = TextBuddy.search(file, new String[] { "search", "xxx" });
    assertEquals("testSearch match error", "0. this is a search string xxx", lines.get(0));
    assertEquals("testSearch match error", "3. The movie xxx", lines.get(1));
  }

}
